<?php

namespace app\common\model\log;

use think\Model;

class XcxApiLogs extends Model {
    protected $connection = ['database' => 'jooj_log'];
    protected $table = 'jj_xcx_api_logs';
    private $_baseTable = 'jj_xcx_api_logs';
    /**
     * 构造方法，创建不同分表的模型
     * @param int $companyId 集团ID
     */
    public function __construct() {
        $date = date('Ymd', time());
        $this->table = $this->_baseTable . '_' . $date;
        parent::__construct();
        $this->_autoCreateTable();
    }

    /**
     * 判断表是否存在，不存在就要自动创建
     * @param type $tableName
     * @return boolean
     */
    private function _autoCreateTable() {
        //判断这张表存不存在，不存在就要创建
        $create_sql = "CREATE TABLE IF NOT EXISTS " .$this->table . " LIKE " .$this->_baseTable;
        $result = $this->execute($create_sql);
        if (false === $result) {
            die('表建立失败，语句如下：' . $this->getLastSql() . "\n\n");
        } 

    }
    
    //写入接口日志
    public function write_log($param,$result,$status,$url,$ip) {
        $data = [
            'url'=>$url,
            'param'=>$param,
            'result'=>$result,
            'status'=>$status,
            'log_time'=>time(),
            'ip'=>$ip
        ];
        $rs = $this->insert($data);
        if($rs){
           $rs = 'success';
        }else{
           $rs = 'fail';
        }
        return $rs;
    }

}
